fn main() {
    println!("==========================");
    test01_simple_use();
    println!("==========================");
    test02_recursive();
    println!("==========================");
    test03_deref();
    println!("==========================");
    test04_use_box_as_ref();
    println!("==========================");
    test05_mybox();
    println!("==========================");
}

fn test01_simple_use() {
    let b = Box::new(5);
    println!("b={b}");
}

#[derive(Debug)]
enum List {
    Cons(i32, Box<List>),
    Nil,
}
use std::ops::Deref;

use crate::List::{Cons, Nil};

fn test02_recursive() {
    let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil))))));
    println!("{list:?}");
}

fn test03_deref() {
    let x = 5;
    let y = &x;

    assert_eq!(5, x);
    assert_eq!(5, *y);

    println!("{x}");
    println!("{y}");
}

fn test04_use_box_as_ref() {
    let x = 5;
    let y = Box::new(x);

    assert_eq!(5, x);
    assert_eq!(5, *y);

    println!("{}", y);
}

struct MyBox<T>(T);
impl<T> MyBox<T> {
    fn new(x: T) -> MyBox<T> {
        MyBox(x)
    }
}
impl<T> Deref for MyBox<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

fn hello(name: &str) {
    println!("Hello, {name}!");
}

fn test05_mybox() {
    let x = 5;
    let y = MyBox::new(x);

    assert_eq!(5, x);
    assert_eq!(5, *y);

    println!("{}", *y);

    let m = MyBox::new(String::from("Rust"));
    hello(&m);
    hello(&(*m)[..]);
}
